package vue;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.SpringLayout;

import Modele.Log;


public class LogListRenderer implements ListCellRenderer<Log> {

	private JPanel pnl;
	private JLabel nl;
	private JLabel l1;
	private JLabel l2;
	private JLabel l3;
	private JLabel l4;
	private JLabel l5;
	
	public LogListRenderer()
	{
		pnl = new JPanel();
		//pnl.setLayout(new GridLayout(1,0));
		pnl.setLayout(new FlowLayout());
		//pnl.setLayout(new SpringLayout());
		nl = new JLabel();
		l1 = new JLabel();
		l2 = new JLabel();
		l3 = new JLabel();
		l4 = new JLabel();
		l5 = new JLabel();
		pnl.add(nl);
		pnl.add(l1);
		pnl.add(l2);
		pnl.add(l3);
		pnl.add(l4);
		pnl.add(l5);
	}
	
	public static int c1 = 0;
	public static int c2 = 0;
	public static int c3 = 0;
	public static int c4 = 0;
	public static int c5 = 0;
	public static int tot = 0;
	
	public static void recalculerLargeur(int largeur)
	{
		largeur = largeur - 40;
		if(tot == largeur)
			return;
		tot = largeur;
		
		c1 = Math.min(largeur/4, 125);
		c3 = Math.min(largeur/4, 200);
		c4 = Math.min(largeur/4, 200);
		c5 = Math.min(largeur/4, 350);
		
		c2 = largeur - (c1+c3+c4+c5+50);
	}
	
	@Override
	public Component getListCellRendererComponent(JList<? extends Log> list,
			Log value, int index, boolean isSelected, boolean cellHasFocus) {
	    
		//if(value.baseCommand.display)
		//{
		if(list.getWidth() != tot)
			recalculerLargeur(list.getWidth());
		
		nl.setText(""+value.ligne);
		/*
		l1.setText("<html><p>"+ value.date +"</p></html>");
		l1.setPreferredSize(new Dimension(c1,15));
		l2.setText("<html><p>"+ value.command +"</p></html>");
		l2.setPreferredSize(new Dimension(c2,15));
		l3.setText("<html><p>"+ value.player +"</p></html>");
		l3.setPreferredSize(new Dimension(c3,15));
		l4.setText("<html><p>"+ value.target +"</p></html>");
		l4.setPreferredSize(new Dimension(c4,15));
		l5.setText("<html><p>"+ value.position +"</p></html>");
		l5.setPreferredSize(new Dimension(c5,15));
		*/
		l1.setText(value.date);
		l1.setPreferredSize(new Dimension(c1,15));
		l2.setText(value.command);
		l2.setPreferredSize(new Dimension(c2,15));
		l3.setText(value.player);
		l3.setPreferredSize(new Dimension(c3,15));
		l4.setText(value.target);
		l4.setPreferredSize(new Dimension(c4,15));
		l5.setText(value.position);
		l5.setPreferredSize(new Dimension(c5,15));
		
		if(isSelected)
			pnl.setBackground(Color.LIGHT_GRAY);
		else
			pnl.setBackground(value.baseCommand.importance.color);
			//pnl.setBackground(Coloration.INSATNCE.getCouleur(value.baseCommand.importance));

		if(value.garder)
		{
			nl.setForeground(Color.RED);
		}
		else
		{
			nl.setForeground(Color.DARK_GRAY);
		}
		
		return pnl;
		/*}
		else
		{
			pnl.setVisible(false);
			return pnl;
		}*/
	}

}
